﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using HengTong.Model.Db;
using HengTong.Model;
using HengTong.Model.Db.Hr.Bill;


namespace HengTong.ViewModel.Hr.SysHrPersonEntryBillVms
{
    public partial class SysHrPersonEntryBillListVm : BasePagedListVM<SysHrEntryBillView, SysHrPersonEntryBillSearcher>
    {

        protected override IEnumerable<IGridColumn<SysHrEntryBillView>> InitGridHeader()
        {
            return new List<GridColumn<SysHrEntryBillView>>{
                this.MakeGridHeader(x => x.BillCode),
                this.MakeGridHeader(x => x.Approver),
                this.MakeGridHeader(x => x.ApprovalTime),
                this.MakeGridHeader(x => x.Name),
                this.MakeGridHeader(x => x.StaffNo),
                this.MakeGridHeader(x => x.BirthDate),
                this.MakeGridHeader(x => x.Age),
                this.MakeGridHeader(x => x.Gender),
                this.MakeGridHeader(x => x.Ethnicity),
                this.MakeGridHeader(x => x.HighestEducation),
                this.MakeGridHeader(x => x.MaritalStatus),
                this.MakeGridHeader(x => x.PhotoId).SetFormat(PhotoIdFormat),
                this.MakeGridHeader(x => x.IdCardNo),
                this.MakeGridHeader(x => x.IdCardFrontPhotoId).SetFormat(IdCardFrontPhotoIdFormat),
                this.MakeGridHeader(x => x.IdCardBackPhotoId).SetFormat(IdCardBackPhotoIdFormat),
                this.MakeGridHeader(x => x.HouseholdRegistration),
                this.MakeGridHeader(x => x.NativePlace),
                this.MakeGridHeader(x => x.PoliticalStatus),
                this.MakeGridHeader(x => x.PartyJoinDate),
                this.MakeGridHeader(x => x.PartyBranch),
                this.MakeGridHeader(x => x.ArchiveLocation),
                this.MakeGridHeader(x => x.ArchiveNo),
                this.MakeGridHeader(x => x.ArchiveTransferOutDate),
                this.MakeGridHeader(x => x.ArchiveTransferInDate),
                this.MakeGridHeader(x => x.HomeAddress),
                this.MakeGridHeader(x => x.Phone),
                this.MakeGridHeader(x => x.Email),
                this.MakeGridHeader(x => x.SalaryBank),
                this.MakeGridHeader(x => x.SalaryAccount),
                this.MakeGridHeader(x => x.BonusBank),
                this.MakeGridHeader(x => x.BonusAccount),
                this.MakeGridHeader(x => x.GroupName),
                this.MakeGridHeader(x => x.JobPositionName),
                this.MakeGridHeader(x => x.ResumeId).SetFormat(ResumeIdFormat),
                this.MakeGridHeader(x => x.HireDate),
                this.MakeGridHeader(x => x.ConfirmationDate),
                this.MakeGridHeader(x => x.EmploymentType),
                this.MakeGridHeader(x => x.ContractStartDate),
                this.MakeGridHeader(x => x.ContractType),
                this.MakeGridHeader(x => x.ContractEndDate),
                this.MakeGridHeader(x => x.ResignationDate),
                this.MakeGridHeader(x => x.ResignationReason),
                this.MakeGridHeader(x => x.ResignationType),
                this.MakeGridHeader(x => x.State),
                this.MakeGridHeader(x => x.BloodType),
                this.MakeGridHeader(x => x.Hobbies),
                this.MakeGridHeader(x => x.CanApprove).SetHide().SetFormat((a, b) =>
                {
                    if(a.CanApprove) { return "true"; }
                    else { return "false"; }
                }),

                this.MakeGridHeaderAction(width: 200)
            };
        }
        private List<ColumnFormatInfo> MedicalReportIdFormat(SysHrEntryBillView entity, object val)
        {
            return new List<ColumnFormatInfo>
            {
            };
        }

        private List<ColumnFormatInfo> ScoreReportIdFormat(SysHrEntryBillView entity, object val)
        {
            return new List<ColumnFormatInfo>
            {
            };
        }

        private List<ColumnFormatInfo> PhotoIdFormat(SysHrEntryBillView entity, object val)
        {
            return new List<ColumnFormatInfo>
            {
                ColumnFormatInfo.MakeDownloadButton(ButtonTypesEnum.Button,entity.PhotoId),
                ColumnFormatInfo.MakeViewButton(ButtonTypesEnum.Button,entity.PhotoId,640,480),
            };
        }

        private List<ColumnFormatInfo> IdCardFrontPhotoIdFormat(SysHrEntryBillView entity, object val)
        {
            return new List<ColumnFormatInfo>
            {
                ColumnFormatInfo.MakeDownloadButton(ButtonTypesEnum.Button,entity.IdCardFrontPhotoId),
                ColumnFormatInfo.MakeViewButton(ButtonTypesEnum.Button,entity.IdCardFrontPhotoId,640,480),
            };
        }

        private List<ColumnFormatInfo> IdCardBackPhotoIdFormat(SysHrEntryBillView entity, object val)
        {
            return new List<ColumnFormatInfo>
            {
                ColumnFormatInfo.MakeDownloadButton(ButtonTypesEnum.Button,entity.IdCardBackPhotoId),
                ColumnFormatInfo.MakeViewButton(ButtonTypesEnum.Button,entity.IdCardBackPhotoId,640,480),
            };
        }

        private List<ColumnFormatInfo> ResumeIdFormat(SysHrEntryBillView entity, object val)
        {
            return new List<ColumnFormatInfo>
            {
                ColumnFormatInfo.MakeDownloadButton(ButtonTypesEnum.Button,entity.ResumeId),
                ColumnFormatInfo.MakeViewButton(ButtonTypesEnum.Button,entity.ResumeId,640,480),
            };
        }

    public override void AfterDoSearcher()
    {
        if (Wtm.LoginUserInfo != null)
        {
            var ids = GetMyApproves();
            foreach (var item in EntityList)
            {
                var m = ids.Find(x => x.ModelID == item.GetID().ToString());
                if (m!=null)
                {
                    item.CanApprove = true;
                }
            }
        }
    }


        public override IOrderedQueryable<SysHrEntryBillView> GetSearchQuery()
        {
            var query = DC.Set<SysHrOnboardingRequestBill>()
                .CheckContain(Searcher.BillCode, x=>x.BillCode)
                .CheckContain(Searcher.Approver, x=>x.Approver)
                .CheckBetween(Searcher.ApprovalTime?.GetStartTime(), Searcher.ApprovalTime?.GetEndTime(), x => x.ApprovalTime, includeMax: false)
                .CheckContain(Searcher.Name, x=>x.Name)
                .CheckContain(Searcher.StaffNo, x=>x.StaffNo)
                .CheckBetween(Searcher.BirthDate?.GetStartTime(), Searcher.BirthDate?.GetEndTime(), x => x.BirthDate, includeMax: false)
                .CheckEqual(Searcher.Age, x=>x.Age)
                .CheckEqual(Searcher.Gender, x=>x.Gender)
                .CheckEqual(Searcher.Ethnicity, x=>x.Ethnicity)
                .CheckEqual(Searcher.HighestEducation, x=>x.HighestEducation)
                .CheckEqual(Searcher.MaritalStatus, x=>x.MaritalStatus)
                .CheckContain(Searcher.IdCardNo, x=>x.IdCardNo)
                .CheckContain(Searcher.HouseholdRegistration, x=>x.HouseholdRegistration)
                .CheckContain(Searcher.NativePlace, x=>x.NativePlace)
                .CheckEqual(Searcher.PoliticalStatus, x=>x.PoliticalStatus)
                .CheckBetween(Searcher.PartyJoinDate?.GetStartTime(), Searcher.PartyJoinDate?.GetEndTime(), x => x.PartyJoinDate, includeMax: false)
                .CheckContain(Searcher.PartyBranch, x=>x.PartyBranch)
                .CheckContain(Searcher.ArchiveLocation, x=>x.ArchiveLocation)
                .CheckContain(Searcher.ArchiveNo, x=>x.ArchiveNo)
                .CheckBetween(Searcher.ArchiveTransferOutDate?.GetStartTime(), Searcher.ArchiveTransferOutDate?.GetEndTime(), x => x.ArchiveTransferOutDate, includeMax: false)
                .CheckBetween(Searcher.ArchiveTransferInDate?.GetStartTime(), Searcher.ArchiveTransferInDate?.GetEndTime(), x => x.ArchiveTransferInDate, includeMax: false)
                .CheckContain(Searcher.HomeAddress, x=>x.HomeAddress)
                .CheckContain(Searcher.Phone, x=>x.Phone)
                .CheckContain(Searcher.Email, x=>x.Email)
                .CheckContain(Searcher.SalaryBank, x=>x.SalaryBank)
                .CheckContain(Searcher.SalaryAccount, x=>x.SalaryAccount)
                .CheckContain(Searcher.BonusBank, x=>x.BonusBank)
                .CheckContain(Searcher.BonusAccount, x=>x.BonusAccount)
                .CheckEqual(Searcher.GroupId, x=>x.GroupId)
                .CheckBetween(Searcher.HireDate?.GetStartTime(), Searcher.HireDate?.GetEndTime(), x => x.HireDate, includeMax: false)
                .CheckBetween(Searcher.ConfirmationDate?.GetStartTime(), Searcher.ConfirmationDate?.GetEndTime(), x => x.ConfirmationDate, includeMax: false)
                .CheckEqual(Searcher.EmploymentType, x=>x.EmploymentType)
                .CheckBetween(Searcher.ContractStartDate?.GetStartTime(), Searcher.ContractStartDate?.GetEndTime(), x => x.ContractStartDate, includeMax: false)
                .CheckEqual(Searcher.ContractType, x=>x.ContractType)
                .CheckBetween(Searcher.ContractEndDate?.GetStartTime(), Searcher.ContractEndDate?.GetEndTime(), x => x.ContractEndDate, includeMax: false)
                .CheckBetween(Searcher.ResignationDate?.GetStartTime(), Searcher.ResignationDate?.GetEndTime(), x => x.ResignationDate, includeMax: false)
                .CheckContain(Searcher.ResignationReason, x=>x.ResignationReason)
                .CheckEqual(Searcher.ResignationType, x=>x.ResignationType)
                .CheckEqual(Searcher.State, x=>x.State)
                .CheckEqual(Searcher.BloodType, x=>x.BloodType)
                .CheckContain(Searcher.Hobbies, x=>x.Hobbies)
                .Select(x => new SysHrEntryBillView
                {
				    ID = x.ID,
                    BillCode = x.BillCode,
                    Approver = x.Approver,
                    ApprovalTime = x.ApprovalTime,
                    Name = x.Name,
                    StaffNo = x.StaffNo,
                    BirthDate = x.BirthDate,
                    Age = x.Age,
                    Gender = x.Gender,
                    Ethnicity = x.Ethnicity,
                    HighestEducation = x.HighestEducation,
                    MaritalStatus = x.MaritalStatus,
                    PhotoId = x.PhotoId,
                    IdCardNo = x.IdCardNo,
                    IdCardFrontPhotoId = x.IdCardFrontPhotoId,
                    IdCardBackPhotoId = x.IdCardBackPhotoId,
                    HouseholdRegistration = x.HouseholdRegistration,
                    NativePlace = x.NativePlace,
                    PoliticalStatus = x.PoliticalStatus,
                    PartyJoinDate = x.PartyJoinDate,
                    PartyBranch = x.PartyBranch,
                    ArchiveLocation = x.ArchiveLocation,
                    ArchiveNo = x.ArchiveNo,
                    ArchiveTransferOutDate = x.ArchiveTransferOutDate,
                    ArchiveTransferInDate = x.ArchiveTransferInDate,
                    HomeAddress = x.HomeAddress,
                    Phone = x.Phone,
                    Email = x.Email,
                    SalaryBank = x.SalaryBank,
                    SalaryAccount = x.SalaryAccount,
                    BonusBank = x.BonusBank,
                    BonusAccount = x.BonusAccount,
                    GroupName = x.Group.GroupName,
                    JobPositionName = x.JobPosition.Name,
                    ResumeId = x.ResumeId,
                    HireDate = x.HireDate,
                    ConfirmationDate = x.ConfirmationDate,
                    EmploymentType = x.EmploymentType,
                    ContractStartDate = x.ContractStartDate,
                    ContractType = x.ContractType,
                    ContractEndDate = x.ContractEndDate,
                    ResignationDate = x.ResignationDate,
                    ResignationReason = x.ResignationReason,
                    ResignationType = x.ResignationType,
                    State = x.State,
                    BloodType = x.BloodType,
                    Hobbies = x.Hobbies,
                })
                .OrderBy(x => x.ID);
            return query;
        }

    }

    public class SysHrEntryBillView : SysHrOnboardingRequestBill{
        [Display(Name = "_Admin.GroupName")]
        public String GroupName { get; set; }
        [Display(Name = "岗位名称")]
        public String JobPositionName { get; set; }
        public bool CanApprove { get; set; }


    }
}
